{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "SVG path loading unavailable!\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/musy/soft/anaconda3/lib/python3.6/site-packages/trimesh/path/exchange/svg_io.py\", line 18, in <module>\n",
      "    from svg.path import parse_path\n",
      "ModuleNotFoundError: No module named 'svg'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rays hit the mesh at coordinates:\n",
      " [[ 0.  0. -1.]\n",
      " [ 0.  0.  1.]]\n",
      "The rays with index: [0 0] hit triangles stored at mesh.faces[[1178 1018]]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "913aa42929c6420784bc79eb10fe555c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import trimesh\n",
    "import numpy as np\n",
    "from vtkplotter import show\n",
    "\n",
    "# test on a sphere mesh\n",
    "mesh = trimesh.creation.icosphere()\n",
    "\n",
    "# create some rays\n",
    "ray_origins    = np.array([[0, 0, -3], [1,  2, -3]])\n",
    "ray_directions = np.array([[0, 0,  1], [0, -1,  1]])\n",
    "\n",
    "# run the mesh-ray query\n",
    "locations, index_ray, index_tri = mesh.ray.intersects_location(\n",
    "    ray_origins=ray_origins, ray_directions=ray_directions\n",
    ")\n",
    "locs = trimesh.points.PointCloud(locations)\n",
    "\n",
    "# stack rays into line segments for visualization as Path3D\n",
    "ray_visualize = trimesh.load_path(\n",
    "    np.hstack((ray_origins, ray_origins + ray_directions)).reshape(-1, 2, 3)\n",
    ")\n",
    "\n",
    "print(\"The rays hit the mesh at coordinates:\\n\", locations)\n",
    "print(\"The rays with index: {} hit triangles stored at mesh.faces[{}]\".format(index_ray, index_tri))\n",
    "\n",
    "# stack rays into line segments for visualization as Path3D\n",
    "ray_visualize = trimesh.load_path(\n",
    "    np.hstack((ray_origins, ray_origins + ray_directions * 5.0)).reshape(-1, 2, 3)\n",
    ")\n",
    "\n",
    "# make mesh white-ish\n",
    "mesh.visual.face_colors = [200, 200, 250, 100]\n",
    "mesh.visual.face_colors[index_tri] = [255, 0, 0, 255]\n",
    "\n",
    "show(mesh, ray_visualize, locs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
